package com.cloud.school.bus.teacherhelper.modules.home;

import java.util.Random;

import android.content.Context;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.dialog.CustomWaitDialog.OnKeyCancel;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.cloud.school.bus.teacherhelper.base.utils.PictureUtil;
import com.cloud.school.bus.teacherhelper.protocol.accountrelevant.ModifyLoginPasswordRequest;
import com.cloud.school.bus.teacherhelper.protocol.accountrelevant.ModifyLoginPasswordResponse;
import com.cloud.school.bus.teacherhelper.protocol.accountrelevant.ResetParentsPasswordRequest;
import com.cloud.school.bus.teacherhelper.protocol.accountrelevant.ResetParentsPasswordResponse;
import com.cloud.school.bus.teacherhelper.protocol.classmanage.AddStudentRequest;
import com.cloud.school.bus.teacherhelper.protocol.classmanage.AddStudentResponse;
import com.cloud.school.bus.teacherhelper.protocol.classmanage.InClassRequest;
import com.cloud.school.bus.teacherhelper.protocol.classmanage.InClassResponse;
import com.cloud.school.bus.teacherhelper.protocol.classmanage.SearchStudentRequest;
import com.cloud.school.bus.teacherhelper.protocol.classmanage.SearchStudentResponse;
import com.cloud.school.bus.teacherhelper.protocol.classstudent.ModifyAvatarRequest;
import com.cloud.school.bus.teacherhelper.protocol.classstudent.ModifyAvatarResponse;
import com.cloud.school.bus.teacherhelper.protocol.classstudent.ModifyStudentInfoRequest;
import com.cloud.school.bus.teacherhelper.protocol.classstudent.ModifyStudentInfoResponse;
import com.cloud.school.bus.teacherhelper.protocol.notice.GetNoticeListRequest;
import com.cloud.school.bus.teacherhelper.protocol.notice.GetNoticeListResponse;
import com.cloud.school.bus.teacherhelper.protocol.notice.SendNoticeRequest;
import com.cloud.school.bus.teacherhelper.protocol.notice.SendNoticeResponse;
import com.cloud.school.bus.teacherhelper.protocol.statistics.GetUploadingCountRequest;
import com.cloud.school.bus.teacherhelper.protocol.statistics.GetUploadingCountResponse;
import com.cloud.school.bus.teacherhelper.protocol.statistics.UploadFileRequest;
import com.cloud.school.bus.teacherhelper.protocol.statistics.UploadFileResponse;

public class TestProtocol {
	
	private Context mParentContext;
	
//	private void uploadFile(String picPathString) {
//		String datePicString = PictureUtil.getPicString(picPathString, 512);
//		if (null == datePicString) {
//			return;
//		}
//		String fext = picPathString
//				.substring(picPathString.lastIndexOf(".") + 1);
//		String fname = picPathString
//				.substring(picPathString.lastIndexOf("/") + 1);
//		NetworkClient.getNetworkClient().PostRequest(
//				new UploadFileRequest(mParentContext, datePicString, "article",
//						fname, "49445", (System.currentTimeMillis() / 1000)
//								+ "", "49375", "intro", "tag", "1073"),
//				new UploadFileResponse() {
//					@Override
//					public void onResponse(
//							ResponseHandlerInterface responseHandlerInterface) {
//					}
//				});
//	}

	private void getUploadingCount() {
		NetworkClient.getNetworkClient().GetRequest(
				new GetUploadingCountRequest(mParentContext),
				new GetUploadingCountResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if ("1".equals(code)) {
							DebugLog.logI("mp4zongshu : " + mp4zongshu);
							DebugLog.logI("piczongshu : " + piczongshu);
						}
					}
				});
	}

	private void resetParentsPassword() {
		NetworkClient.getNetworkClient().PostRequest(
				new ResetParentsPasswordRequest(mParentContext, "49474"),
				new ResetParentsPasswordResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if ("1".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"重置家长登陆密码成功");
						} else if ("-2".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"参数不能为空");
						} else if ("-3".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"该家长不存在");
						}
					}
				});
	}

	private void modifyLoginPassword() {
		NetworkClient.getNetworkClient().PostRequest(
				new ModifyLoginPasswordRequest(mParentContext, "222222",
						"111111"), new ModifyLoginPasswordResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if ("1".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"修改密码成功");
						} else if ("-2".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"输入不能为空");
						} else if ("-4".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"新旧密码相同");
						} else if ("-31".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"老密码不能含特殊符号");
						} else if ("-32".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"新密码不能含特殊符号");
						} else if ("-1012".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"密码不正确");
						}
					}
				});
	}

	private void sendNotice() {
		NetworkClient.getNetworkClient().PostRequest(
				new SendNoticeRequest(mParentContext, "title", "content",
						"49445", "0", "", "", "", ""),
				new SendNoticeResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if ("1".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"发送成功");
						}
					}
				});
	}

	private void getNoticeList() {
		NetworkClient.getNetworkClient().GetRequest(
				new GetNoticeListRequest(mParentContext, "0", "0"),
				new GetNoticeListResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if ("1".equals(code)) {
							DebugLog.logI(noticeList.toString());
						} else if ("-2".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"无内容");
						}
					}
				});
	}

	private void addStudent() {
		NetworkClient.getNetworkClient().PostRequest(
				new AddStudentRequest(mParentContext, "父子", "s加s红",
						"18701680736", "1", "2010-01-01", ""),
				new AddStudentResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if ("1".equals(code)) {
							DebugLog.logI(student.toString());
						} else if ("-3".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"该学生已存在机构中");
						} else if ("-4".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"不是老师操纵");
						} else if ("-2".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"性别或生日姓名不能为空");
						}
					}
				});
	}

	private void inClass() {
		final String type = "0";
		NetworkClient.getNetworkClient().GetRequest(
				new InClassRequest(mParentContext, "49431", "49375", type),
				new InClassResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if ("1".equals(code)) {
							if ("1".equals(type)) {
								// 离班
								HandlerToastUI.getHandlerToastUI(
										mParentContext, "离班成功");
							} else if ("0".equals(type)) {
								// 调班
								HandlerToastUI.getHandlerToastUI(
										mParentContext, "调班成功");
							}
						} else if ("-3".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"studentid 为空");
						} else if ("-5".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"studentuid 为空");
						} else if ("-2".equals(code)) {
							if ("1".equals(type)) {
								// 离班
								HandlerToastUI.getHandlerToastUI(
										mParentContext, "该学生不在本班级");
							} else if ("0".equals(type)) {
								// 调班
								HandlerToastUI.getHandlerToastUI(
										mParentContext, "该学生已在本班级");
							}

						}
					}
				});
	}

	private void searchStudent() {
		NetworkClient.getNetworkClient().GetRequest(
				new SearchStudentRequest(mParentContext, "陈佳", "18701680736"),
				new SearchStudentResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if ("1".equals(code)) {
							DebugLog.logI(studentList.toString());
						} else if ("-3".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"输入不能为空");
						} else if ("-2".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"手机号格式不正确");
						}
					}
				});
	}

	private void modifyStudentInfo() {
		NetworkClient.getNetworkClient().PostRequest(
				new ModifyStudentInfoRequest(mParentContext, "贾鸿飞", "1",
						"2012-01-01", "49431", "49375", "enname"),
				new ModifyStudentInfoResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if ("1".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"修改学生信息成功");
						} else if ("-3".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"studentid为空");
						} else if ("-1015".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"班级中没有改学生");
						} else if ("-2".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"性别或生日为空");
						}
					}
				});
	}

	private void modifyAvatar(String picPathString) {
		// "studentid": "49431"
//		showWaitDialog("正在上传请稍后...", new OnKeyCancel() {
//
//			@Override
//			public void onKeyCancelListener() {
//				NetworkClient.getNetworkClient().cancelRequests(mParentContext,
//						true);
//			}
//		});
		String datePicString = PictureUtil.getPicString(picPathString, 512);
		if (null == datePicString) {
			return;
		}
		String fext = picPathString
				.substring(picPathString.lastIndexOf(".") + 1);
		NetworkClient.getNetworkClient().PostRequest(
				new ModifyAvatarRequest(mParentContext, datePicString, fext,
						"", "49431"), new ModifyAvatarResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if ("1".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"修改学生头像成功");
							DebugLog.logI("avatarUrl : " + avatarUrl);
						} else if ("-36".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"学生id为空");
						} else if ("-40".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"文件流为空");
						} else if ("-42".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mParentContext,
									"文件已存在");
						}
						// if (TextUtils.isEmpty(responseString)) {
						// HandlerToastUI.getHandlerToastUI(mParentContext,
						// "头像上传失败");
						// } else {
						// DebugLog.logI("responseString : " + responseString);
						// }
						HandlerPostUI.getHandlerPostUI(new Runnable() {

							@Override
							public void run() {
//								hideWaitDialog();
							}
						});
					}
				});
	}
}
